package com.llkj.contractmanagement.contractmanagement.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/16 9:40
 * description:修改密码界面
 */
public class EditPassWordActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.et_pw_old)
    EditText etPwOld;
    @Bind(R.id.et_pw_new)
    EditText etPwNew;
    @Bind(R.id.et_pw_again)
    EditText etPwAgain;
    @Bind(R.id.btn_finish)
    Button btnFinish;
    @Bind(R.id.title_edit_pw)
    TitleBar titleBar;

    private String pwOld = "";
    private String pwNew = "";
    private String pwAgain = "";

    private UserInfoBean userInfoBean;
    private int type = 0 ;
    private SharedPreferences sp;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_editpassword;
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        initData();
        setListener();
    }

    private void initData() {
        //获得实例对象
        sp = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userInfoBean = UserInfoBean.getInstance();
        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            titleBar.setLeft_background(null);
        }
    }

    private void setListener() {
        btnFinish.setOnClickListener(this);
        titleBar.setTopBarClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_finish:
                editPW();
                break;
        }
    }

    private void editPW() {
        pwOld = etPwOld.getText().toString().trim();
        pwNew = etPwNew.getText().toString().trim();
        pwAgain = etPwAgain.getText().toString().trim();
        if (TextUtils.isEmpty(pwOld)) {
            ToastUtil.makeShortText(EditPassWordActivity.this, getString(R.string.please_input_pw_old));
            return;
        }
        if (TextUtils.isEmpty(pwNew)) {
            ToastUtil.makeShortText(EditPassWordActivity.this, getString(R.string.please_input_pw_new));
            return;
        }
        if (pwNew.length() < 6) {
            ToastUtil.makeShortText(EditPassWordActivity.this, getString(R.string.please_input_pw_new));
            return;
        }

        if (TextUtils.isEmpty(pwAgain)) {
            ToastUtil.makeShortText(EditPassWordActivity.this, getString(R.string.please_input_pw_again));
            return;
        }
        if (pwAgain.equals("123456") || pwNew.equals("123456")) {
            ToastUtil.makeShortText(EditPassWordActivity.this, "新密码不能与初始密码相同");
            return;
        }

        if (pwAgain.length() < 6) {
            ToastUtil.makeShortText(EditPassWordActivity.this, getString(R.string.please_input_pw_again));
            return;
        }

        if (!pwNew.equals(pwAgain)) {
            ToastUtil.makeShortText(EditPassWordActivity.this, getString(R.string.check_pw_false_input_again));
            return;
        }
//        user_id	1	string	用户id
//        password	1	string	旧密码
//        newpassword	1	string	新密码
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id", userInfoBean.getId());
        map.put("password", pwOld);
        map.put("newpassword", pwNew);
        HttpMethod.updPassword(this, this, map, UrlConfig.HTTP_UPDPASSWORD);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        if (resultCode == UrlConfig.HTTP_UPDPASSWORD) {
            ToastUtil.makeShortText(this, "密码修改成功");

            SharedPreferences.Editor editor = sp.edit();
            editor.putString("PASSWORD", pwNew);
            editor.commit();
            if (type == 1) {
                userInfoBean.clearUserInfo();
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                this.finish();
            } else if (type == 0) {
                //记住用户名、密码、
                this.finish();
            }
        }
    }

    @Override
    public void onFailureHttp(String message) {
        super.onFailureHttp(message);
        ToastUtil.makeShortText(this, message);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
